﻿package org.dm.3d.Actions.object 
{
	import org.dm.3d.Actions.Action;
	import org.dm.3d.geom.Num3D;
	import org.dm.3d.geom.MathExtend;
	
	/**
	 * ...
	 * @author Scott
	 */
	public class AL1 extends Action
	{
		private var goalPos:Array;
		private var goalNum:uint;
		private var _init:Boolean = false;
		private var _pre:Boolean = true;
		private var _root:Boolean = false;
		private var _cur:uint = 0;
		private var _speed:uint = 12;
		
		public function AL1() 
		{
			this.isUsed = true;
			goalPos = new Array();
			var points:Vector.<Number> = Vector.<Number>([-388.5,107,-382.5,91,-377.5,107,-376.5,76,-369.5,62,-365.5,107,-362.5,48,-360.5,94,-355.5,34,-353.5,80,-348.5,19,-347.5,66,-342.5,5,-340.5,52,-335.5,-9,-333.5,38,-328.5,-24,-327.5,24,-321.5,-38,-320.5,11,-315.5,-52,-314.5,-3,-308.5,-66,-307.5,-17,-301.5,-81,-300.5,-31,-294.5,-95,-294.5,-45,-286.5,-61,-285.5,-108,-277.5,-95,-277.5,-45,-271.5,-31,-270.5,-81,-264.5,-17,-263.5,-66,-258.5,-3,-257.5,-52,-251.5,11,-250.5,-38,-244.5,24,-243.5,-24,-238.5,38,-236.5,-9,-231.5,52,-230.5,5,-225.5,66,-223.5,19,-218.5,80,-216.5,34,-212.5,94,-210.5,48,-205.5,107,-203.5,62,-196.5,76,-194.5,107,-190.5,91,-182.5,107,-84.5,62,-82.5,47,-81.5,77,-76.5,32,-73.5,91,-68.5,19,-61.5,65,-61.5,101,-60.5,50,-57.5,-70,-57.5,-54,-57.5,7,-54.5,79,-53.5,-85,-53.5,-39,-53.5,36,-47.5,107,-46.5,-4,-44.5,-98,-44.5,-26,-44.5,24,-42.5,87,-36.5,-70,-34.5,-53,-34.5,-13,-33.5,13,-32.5,110,-31.5,-107,-29.5,-84,-27.5,90,-25.5,-39,-20.5,3,-16.5,-111,-16.5,109,-15.5,-27,-14.5,-91,-12.5,16,-11.5,88,-3.5,-35,-2.5,-7,-2.5,28,-1.5,106,-0.5,-111,1.5,-89,2.5,82,6.5,6,6.5,40,8.5,-17,9.5,-46,13.5,-78,13.5,101,14.5,-107,15.5,-62,15.5,74,16.5,19,16.5,52,20.5,-27,26.5,31,26.5,62,27.5,-98,27.5,93,30.5,-39,35.5,-84,36.5,-53,38.5,-69,38.5,41,38.5,85,46.5,24,49.5,95,50.5,4,53.5,65,60.5,4,60.5,49,60.5,106,65.5,34,65.5,75,69.5,19,71.5,4,72.5,106,77.5,89,88.5,106,231.5,-108,231.5,-93,231.5,-77,231.5,-62,231.5,-47,231.5,-31,231.5,-16,231.5,-1,231.5,15,231.5,30,231.5,45,231.5,61,231.5,76,231.5,91,231.5,106,241.5,-108,246.5,107,252.5,-92,252.5,-76,252.5,-60,252.5,-43,252.5,-27,252.5,-11,252.5,5,252.5,21,252.5,37,252.5,54,252.5,70,253.5,-108,253.5,86,262.5,107,269.5,86,277.5,107,286.5,86,293.5,107,303.5,86,309.5,107,319.5,86,324.5,107,336.5,86,340.5,107,353.5,86,355.5,107,370.5,86,371.5,107,387.5,86,387.5,96,387.5,107]);
			for (var i:uint = 0; i < points.length/2; i++)
			{
				goalPos.push(new Num3D(points[2 * i], points[2 * i + 1],0));
				goalPos.push(new Num3D(points[2 * i], points[2 * i + 1],20));
			}
			goalNum = goalPos.length;
		}
		public override function update(items:Array,num:uint):void 
		{
			_cur++;
			/*if (_cur == 1);
			{
				var tem:Array = new Array();
				for (i = 0; i < num; i++)
				{
					tem.push(items[i].pos);
				}
				tem.sortOn("z", Array.NUMERIC | Array.UNIQUESORT);
				for (i = 0; i < num; i++)
				{
					items[i].pos = tem[i];
				}
			}*/
			if (_cur < 10)
			{
				if (_pre) preMove(items, num, 1000);				
				return;
			}
			if (_cur<11)
			{
				for (var i:uint = 0; i < num; i++)
				{
					items[i].speed = new Num3D();
					items[i].opos = new Num3D();
					items[i].acceleration = new Num3D();
					items[i].damping = .95;					
				}
				
			}
/*			for (var j:uint = 0; j < 5; j++)
			{
				//if (j > 0) _cur++;
				if (_cur < goalNum + 10+j) 
				{
					addSpeed(items,_cur-10+j);
				}
				if (_cur >= 20+j && _cur < goalNum + 10+j) 
				{
					items[_cur-20+j].speed = new Vector3D();
				}				
			}*/
			if (_cur < 20) return;
			if (_cur < 21+goalNum/_speed) 
			{
				addSpeed(items,(_cur-20)*_speed);
			}
			if (_cur >= 30  ) 
			{
				
				var kmax:uint = (_cur - 30) * _speed ;
				var addk:uint = _speed;
				if (kmax >goalNum)
				{
					addk =  goalNum + _speed-kmax;
					kmax = goalNum;
				}
				var tspeed:Num3D;
				var w:Number;
				var h:Number;
				if(kmax<goalNum)
					for (var k:uint = 0; k < addk; k++)
					{
						//items[(_cur - 30) * _speed + k].speed = new Vector3D();
						items[(_cur-30)*_speed+k].opos = items[(_cur-30)*_speed+k].pos.clone();
					}
				for (k=0; k < kmax; k++)
				{
					tspeed = items[k].opos.subtract(items[k].pos);
					items[k].damping = .85;
					tspeed.scaleBy(.1);
					items[k].acceleration = tspeed;
				}/**/
			}	
		}
		
		public function addSpeed(items:Array,pcur:uint):void 
		{
			var left:uint = pcur + _speed;
			left = left > goalNum?goalNum:left;
			for (var i:uint = pcur; i < left;i++ )
			{
				var damping:Number = items[i].damping;
				damping = (1 - damping) / (1 - Math.pow(damping, 10));
				var tvc:Num3D = goalPos[i].subtract(items[i].pos);
				tvc.scaleBy(damping);
				items[i].speed = tvc;
				//items[i].pos = goalPos[i];
			}
			/*items[pcur].speed = goalPos[pcur];*/
		}
		public function preMove(items:Array,num:uint,distance:Number):void 
		{
			var tvc:Num3D;
			var damping:Number;
			damping = .95;
			damping = (1 - damping) / (1 - Math.pow(damping, 10));
			for (var i:uint = goalNum; i < num; i++)
			{
				tvc = MathExtend.pInRound(i, 50, distance*.4+distance*.6*Math.random());
				tvc.decrementBy(items[i].pos);
				tvc.scaleBy(damping);
				items[i].speed = tvc;
			}
			this._pre = false;
		}
	}
	
}